package server

import (
	"context"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"time"
)

// NewMinioClient 创建一个minio Client的实例对象
func NewMinioClient(endpoint, accessKey, secretKey string, useSSL bool) (*minio.Client, error) {
	return minio.New(endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKey, secretKey, ""),
		Secure: useSSL,
	})
}

// GetPresignedPutURL 生成预签名上传URL
func GetPresignedPutURL(client *minio.Client, bucket, objectName string, expireSeconds int64) (string, error) {
	u, err := client.PresignedPutObject(context.Background(), bucket, objectName, time.Duration(expireSeconds)*time.Second)
	if err != nil {
		return "", err
	}
	return u.String(), nil
}
